package com.ali.service.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 事务传播
 */
@Slf4j
@Service
public class TransactionalSpreadServiceImpl {
    @Resource
    private TransactionalFirstServiceImpl transactionalFirstService;
    @Resource
    private TransactionalSecondServiceImpl transactionalSecondService;

    @Transactional
    public void required() {
        String content = "required";
        transactionalFirstService.save(content);
        transactionalSecondService.required(content);
        transactionalSecondService.requiredException(content);
    }

    @Transactional
    public void supports() {
        String content = "supports";
        transactionalFirstService.save(content);
        transactionalSecondService.supports(content);
        transactionalSecondService.supportsException(content);
    }

    @Transactional
    public void mandatory() {
        String content = "mandatory";
        transactionalFirstService.save(content);
        transactionalSecondService.mandatory(content);
        transactionalSecondService.mandatoryException(content);
    }

    /**
     * 子事务不单独回滚
     */
    @Transactional
    public void requiresNew1() {
        String content = "requiresNew1";
        transactionalFirstService.save(content);
        transactionalSecondService.requiresNew(content);
        transactionalSecondService.requiresNewException(content);
    }

    /**
     * 子事务单独回滚
     */
    @Transactional
    public void requiresNew2() {
        String content = "requiresNew2";
        transactionalFirstService.save(content);
        transactionalSecondService.requiresNew(content);
        try{
            transactionalSecondService.requiresNewException(content);
        }catch (Exception e){
            log.info("子事务异常");
        }
    }

    @Transactional
    public void notSupported() {
        String content = "notSupported";
        transactionalFirstService.save(content);
        transactionalSecondService.notSupported(content);
        transactionalSecondService.notSupportedException(content);
    }

    @Transactional
    public void never() {
        String content = "never";
        transactionalFirstService.save(content);
        transactionalSecondService.never(content);
        transactionalSecondService.neverException(content);
    }

    /**
     * 子事务不单独回滚
     */
    @Transactional
    public void nested1() {
        String content = "nested1";
        transactionalFirstService.save(content);
        transactionalSecondService.nested(content);
        transactionalSecondService.nestedException(content);
    }

    /**
     * 子事务单独回滚
     */
    @Transactional
    public void nested2() {
        String content = "nested2";
        transactionalFirstService.save(content);
        transactionalSecondService.nested(content);
        try {
            transactionalSecondService.nestedException(content);
        } catch (Exception e) {
            log.info("子事务异常");
        }
    }
}
